package ar.uba.fi.taller2.cmmiassistant.frontend;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.impl.Log4jFactory;

import sun.util.logging.resources.logging;

import ar.uba.fi.taller2.cmmiassistant.frontend.util.AssocObject;
import ar.uba.fi.taller2.cmmiassistant.frontend.util.ListAssocObject;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;


public class Practicas extends JPanel {
	
    private static final Log log = LogFactory.getLog(Practicas.class);
    
	private JLabel nombreLabel;
	private JList lstPracticas;
	private JButton btnSave;
	private JButton btnSavenNew;
	private JTextArea fmtNombre;

	
	// Business Att
	Map mapAssocObj;
	
	public Practicas(String title) {
		super();
		setLayout(new FormLayout(
			new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				new ColumnSpec("right:default"),
				FormFactory.RELATED_GAP_COLSPEC,
				new ColumnSpec("default:grow(1.0)"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC}));

		nombreLabel = new JLabel();
		nombreLabel.setText("Nombre:");
		add(nombreLabel, new CellConstraints(2, 2));

		fmtNombre = new JTextArea();
		fmtNombre.setBorder(new LineBorder(Color.black, 1, false));
		add(fmtNombre, new CellConstraints(4, 2));

		lstPracticas = new JList();
		lstPracticas.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent sel) {
				Object obj = getModel().getElementAt(lstPracticas.getSelectedIndex());
				if (obj!=null)
					fmtNombre.setText(obj.toString());
			}
		});
		lstPracticas.setBorder(new TitledBorder(new LineBorder(Color.black, 1, false), title, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
		add(lstPracticas, new CellConstraints(2, 4, 5, 1));

		final JPanel panel = new JPanel();
		panel.setLayout(new FormLayout(
			new ColumnSpec[] {
				new ColumnSpec("default:grow(1.0)"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC},
			new RowSpec[] {
				FormFactory.DEFAULT_ROWSPEC}));
		add(panel, new CellConstraints(2, 6, 5, 1));

		btnSave = new JButton();
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String nomb = fmtNombre.getText();
				if (nomb==null || nomb.equals(""))
				    
				    log.debug("invalid value");
				else {
					AssocObject assoc = (AssocObject)getModel().getElementAt(lstPracticas.getSelectedIndex());
					assoc.setName(nomb);
					lstPracticas.updateUI();
				}
			}
		});
		btnSave.setText("Save");
		panel.add(btnSave, new CellConstraints(5, 1));

		btnSavenNew = new JButton();
		btnSavenNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String nomb = fmtNombre.getText();
				if (nomb==null || nomb.equals(""))
					log.debug("invalid value");
				else {
					int newID = getModel().getSize() +1;
					AssocObject obj = new AssocObject(new Integer(newID),nomb);
					getModel().addAssocObject(obj);
					fmtNombre.setText("");
					lstPracticas.updateUI();
				}
				
			}
		});
		btnSavenNew.setText("Save & New");
		panel.add(btnSavenNew, new CellConstraints(3, 1));

	
	}

	public void initUI(Map mapAssocObj){
		this.mapAssocObj = mapAssocObj;
	}
	
	public void loadUI(Integer id){
		// lista de assoc obj
		ListAssocObject objs = (ListAssocObject)mapAssocObj.get(id);		
		lstPracticas.setModel(objs);
	}
	
	public ListAssocObject getModel(){
		return ((ListAssocObject)lstPracticas.getModel());
	}
	
}
